//
//  BudgetTotalSetView.swift
//  KoiAccounting
//  月总预算设置
//  Created by 纪洪文 on 16.04.25.
//

import SwiftUI

struct BudgetSetTotalView: View {
    
    @State var showDeleteConfirm = false
    
    @State var inputAmount = "0.00"
    
    @State var reset = false
    
    var totalAmount: Double
    
    var onDoneClick: (String) -> Void
    
    var onDeleteAllClick: () -> Void
    
    var body: some View {
        
        VStack {
            VStack (alignment:.leading){
                HStack {
                    Text("设置每月总预算：")
                        .font(.system(size: 14))
                        .foregroundColor(Color.koiColorTitleSecond)
                    Spacer()
                }
                HStack {
                    TextField("请输入预算金额", text: $inputAmount)
                        .font(.system(size: 34))
                        .fontWeight(.medium)
                        .foregroundColor(Color.koiColorPrimary)
                    Spacer()
                    Button(action: {
                        showDeleteConfirm = true
                    }) {
                        Text("清除预算")
                            .font(.system(size: 13))
                            .foregroundColor(Color.koiColorPrimary)
                            .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
                            .background(Color.koiColorPrimary.opacity(0.04))
                            .cornerRadius(16)
                    }
                }
            }
            .padding(EdgeInsets(top: 10, leading: 20, bottom: 10, trailing: 20))
            .cornerRadius(16)
            
            NumericKeypadView(
                numericClickCallback: { inputText in
                    inputAmount = inputText
                },
                doneClickCallback: {
                    onDoneClick(inputAmount)
                },
                reset: $reset
            )
            .frame(height: 264)
            .padding(16)
            .background(Color.koiColorBackgroundItem)
            .cornerRadius(16)
        }
        // 去除边距
        .ignoresSafeArea(.container, edges: .bottom)
        .onAppear {
            inputAmount = totalAmount.toAmountTwoDecimalPlaces()
        }
        .actionSheet(isPresented: $showDeleteConfirm) {
            ActionSheet(title: Text("确认删除总预算分类? 连同全部分类预算也会一起删除"), buttons: [
                .destructive(Text("删除"), action: {
                    onDeleteAllClick()
                }),
                .cancel(Text("取消"))
            ])
        }
    }
}

#Preview {
    BudgetSetTotalView(totalAmount: 0.0, onDoneClick: { _ in }, onDeleteAllClick: {})
}
